package cn.iocoder.yudao.module.media.controller.admin.macvod;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.media.controller.admin.macvod.vo.*;
import cn.iocoder.yudao.module.media.dal.dataobject.macvod.MacVodDO;
import cn.iocoder.yudao.module.media.service.macvod.MacVodService;

@Tag(name = "管理后台 - 电影信息")
@RestController
@RequestMapping("/media/mac-vod")
@Validated
public class MacVodController {

    @Resource
    private MacVodService macVodService;

    @PostMapping("/create")
    @Operation(summary = "创建电影信息")
    @PreAuthorize("@ss.hasPermission('media:mac-vod:create')")
    public CommonResult<Long> createMacVod(@Valid @RequestBody MacVodSaveReqVO createReqVO) {
        return success(macVodService.createMacVod(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新电影信息")
    @PreAuthorize("@ss.hasPermission('media:mac-vod:update')")
    public CommonResult<Boolean> updateMacVod(@Valid @RequestBody MacVodSaveReqVO updateReqVO) {
        macVodService.updateMacVod(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除电影信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('media:mac-vod:delete')")
    public CommonResult<Boolean> deleteMacVod(@RequestParam("id") Long id) {
        macVodService.deleteMacVod(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得电影信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('media:mac-vod:query')")
    public CommonResult<MacVodRespVO> getMacVod(@RequestParam("id") Long id) {
        MacVodDO macVod = macVodService.getMacVod(id);
        return success(BeanUtils.toBean(macVod, MacVodRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得电影信息分页")
    @PreAuthorize("@ss.hasPermission('media:mac-vod:query')")
    public CommonResult<PageResult<MacVodRespVO>> getMacVodPage(@Valid MacVodPageReqVO pageReqVO) {
        PageResult<MacVodDO> pageResult = macVodService.getMacVodPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MacVodRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出电影信息 Excel")
    @PreAuthorize("@ss.hasPermission('media:mac-vod:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMacVodExcel(@Valid MacVodPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MacVodDO> list = macVodService.getMacVodPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "电影信息.xls", "数据", MacVodRespVO.class,
                        BeanUtils.toBean(list, MacVodRespVO.class));
    }

}